package com.regulareport.web.controller.report;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.regulareport.common.annotation.Log;
import com.regulareport.common.core.controller.BaseController;
import com.regulareport.common.core.domain.AjaxResult;
import com.regulareport.common.core.domain.entity.ReportCellDefinition;
import com.regulareport.common.core.page.TableDataInfo;
import com.regulareport.common.enums.BusinessType;
import com.regulareport.common.utils.StringUtils;
import com.regulareport.common.utils.poi.ExcelUtil;
import com.regulareport.form.service.IReportCellDefinitionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 单元格定义Controller
 * 
 * @author ruoyi
 * @date 2025-09-03
 */
@RestController
@RequestMapping("/system/definition")
public class ReportCellDefinitionController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(ReportCellDefinitionController.class);

    @Autowired
    private IReportCellDefinitionService reportCellDefinitionService;

    /**
     * 查询单元格定义列表
     */
    @PreAuthorize("@ss.hasPermi('report:indexDefine:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportCellDefinition reportCellDefinition)
    {

        startPage();
        List<ReportCellDefinition> list = reportCellDefinitionService.selectReportCellDefinitionList(reportCellDefinition);
        return getDataTable(list);
    }

    /**
     * 导出单元格定义列表
     */
    @PreAuthorize("@ss.hasPermi('report:indexDefine:export')")
    @Log(title = "单元格定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportCellDefinition reportCellDefinition)
    {
        List<ReportCellDefinition> list = reportCellDefinitionService.selectReportCellDefinitionList(reportCellDefinition);
        ExcelUtil<ReportCellDefinition> util = new ExcelUtil<ReportCellDefinition>(ReportCellDefinition.class);
        util.exportExcel(response, list, "单元格定义数据");
    }

    /**
     * 获取单元格定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:indexDefine:query')")
    @GetMapping(value = "/search")
    public AjaxResult getInfo(@RequestParam("reportFormID") String reportFormID, @RequestParam("cellAddress") String cellAddress) throws Exception
    {
        return success(reportCellDefinitionService.selectReportCellDefinitionByCompositeKeys(reportFormID, cellAddress));
    }

    @PreAuthorize("@ss.hasPermi('report:indexDefine:query')")
    @GetMapping(value = "/queryByReportFormName")
    public AjaxResult getInfo(@RequestParam("reportFormName") String reportFormName) throws Exception
    {
        return success(reportCellDefinitionService.selectReportCellDefinitionByReportFormName(reportFormName));
    }

    /**
     * 新增单元格定义
     */
    @PreAuthorize("@ss.hasPermi('report:indexDefine:add')")
    @Log(title = "单元格定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map requestMap) throws Exception {
        log.info("单元格定义方法开始，{},  {}, {},  {},  {}, {}, {},  {}", (String) requestMap.get("mergeCell"), (String) requestMap.get("indexCell"),
                (String) requestMap.get("identifierRow"), (String) requestMap.get("desRow"), (String) requestMap.get("reportFormNo"),
                (String) requestMap.get("edition"), String.valueOf(requestMap.get("reportFormID")), String.valueOf(requestMap.get("indexDesSecCol")));

        ReportCellDefinition reportCellDefinition = new ReportCellDefinition();
        String mergeCellAddr = (String) requestMap.get("mergeCell"); //例 B2:B8
        String indexCellAddr = (String) requestMap.get("indexCell"); //例 B2:B8
        String identifierRowAddr = (String) requestMap.get("identifierRow"); //例 C2:E2
        String desRowAddr = (String) requestMap.get("desRow"); //例 C3:E5
        String reportFormNo = (String) requestMap.get("reportFormNo");
        String edition = requestMap.get("edition") == null ? "" : (String) requestMap.get("edition");
        String  reportFormID = String.valueOf(requestMap.get("reportFormID"));

        String  indexDesSecCol = requestMap.get("indexDesSecCol") == null ? "" : String.valueOf(requestMap.get("indexDesSecCol"));
        reportCellDefinition.setIndexNumFirRow(identifierRowAddr.trim());
        reportCellDefinition.setIndexDesFirRow(desRowAddr.trim());
        reportCellDefinition.setIndexDesFirCol(indexCellAddr.trim());
        reportCellDefinition.setCellRow(mergeCellAddr.trim());
        reportCellDefinition.setReportFormNo(reportFormNo);
        reportCellDefinition.setEdition(edition);
        reportCellDefinition.setCreateBy(getUsername());
        reportCellDefinition.setReportFormID(Long.valueOf(reportFormID));
        reportCellDefinition.setIndexDesSecCol(indexDesSecCol);
        if(StringUtils.isEmpty(mergeCellAddr)){
            throw new Exception("未收到选中的单元格");
        }
        if(StringUtils.isEmpty(desRowAddr)){
            throw new Exception("未收到名称命名行信息");
        }
        if(StringUtils.isEmpty(identifierRowAddr)){
            throw new Exception("未收到编号行信息");
        }
        if(StringUtils.isEmpty(indexCellAddr)){
            throw new Exception("未收到编号和名称列信息");
        }

        String affectedRows = reportCellDefinitionService.insertReportCellDefinition(reportCellDefinition);
        log.info("单元格定义方法结束");
        return AjaxResult.success(affectedRows);
    }

    /**
     * 修改单元格定义
     */
    @PreAuthorize("@ss.hasPermi('report:indexDefine:edit')")
    @Log(title = "单元格定义", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ReportCellDefinition reportCellDefinition) throws Exception
    {
        if(StringUtils.isEmpty(reportCellDefinition.getCellRow())){
            throw new Exception("未收到选中的表格范围");
        }
        if(reportCellDefinition.getReportFormID() == null){
            throw new Exception("未收到模板ID");
        }
        return toAjax(reportCellDefinitionService.updateReportCellDefinition(reportCellDefinition));
    }

}
